#!/usr/bin/env python
# -*- coding: utf-8 -*-
""".cog file generator

Sample output:
COG0001 0 H <Glutamate-1-semialdehyde aminotransferase>
COG0002 0 E <Acetylglutamate semialdehyde dehydrogenase>
COG0003 0 P <Oxyanion-translocating ATPase>
COG0004 0 P <Ammonia permease>


"""
__author__ = "Lei Chen <hide1713@gmail.com>"
from pprint import pprint
import sys

if __name__ == '__main__':
    if len(sys.argv)<3:
        print "Cog file generator"
        print "Usage: ./cog_file_generator.py Cog.list length_file output_file"
        sys.exit(0)
    
    f = open(sys.argv[1],'r')
    cog_dict = {}
    for line in f:
        t = line.split("]")[0][1:]
        cog = line.split()[1]
        desc = " ".join(line.split()[2:])
        desc = "<"+desc.strip()+">"
        cog_dict[int(cog[3:])]=[cog,'0',t,desc]
    f.close()
    #pprint(cog_dict)

    length_f = open(sys.argv[2],'r')
    for line in length_f:
        if line.startswith("#"):
            continue
        cog,length,exp_length = line.split()
        cog_dict[int(cog[3:])][1]=length
    length_f.close()

    output = open(sys.argv[3],'w')
    cog_dict.keys().sort()
    for key in cog_dict:
        s = cog_dict[key]
        output.write(" ".join(s)+"\n")
    output.close()
  
